﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Coffee_Restaurant.Database;
using Coffee_Restaurant.Models;
using MetroFramework.Forms;

namespace Coffee_Restaurant.Forms.Items
{
    public partial class UpdateItem : MetroForm
    {
        public delegate void ButtonSaveEventHandler();
        public event ButtonSaveEventHandler BtnSave;
        public UpdateItem()
        {
            InitializeComponent();
        }

        public int ItemIdSelect { get; set; }

        private void UpdateItem_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'coffeeRestaurantDataSet.ItemUnits' table. You can move, or remove it, as needed.
            this.itemUnitsTableAdapter.Fill(this.coffeeRestaurantDataSet.ItemUnits);
            // TODO: This line of code loads data into the 'coffeeRestaurantDataSet.ItemCategories' table. You can move, or remove it, as needed.
            this.itemCategoriesTableAdapter.Fill(this.coffeeRestaurantDataSet.ItemCategories);

        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                try
                {
                    using (var db = new DatabaseEntities())
                    {
                        //save to database
                        var model = db.Items.FirstOrDefault(x => x.ItemId == ItemIdSelect);
                        if (model != null)
                        {
                            model.ItemCategoriesId = int.Parse(cbbCategory.SelectedValue.ToString());
                            model.Name = txtName.Text;
                            model.Price = Decimal.Parse(txtPrice.Text);
                            model.Unit = cbbDVT.Text;
                            model.InWarehouse = 0;
                        }
                        if (db.SaveChanges() > 0)
                        {
                            if (model != null) BtnSave();
                            this.Hide();
                        }
                    }
                }
                catch (Exception)
                {
                    BtnSave();
                }
            }
        }

        bool ValidateForm()
        {
            if (!ValidateHelper.IsString(txtName.Text, 2))
            {
                txtName.Focus();
                return false;
            }
            if (!ValidateHelper.IsNumberOrDecimals(txtPrice.Text, 4))
            {
                txtPrice.Focus();
                return false;
            }
            return true;
        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            lblPriceFormat.Text = ValidateHelper.TryParseDecimal(txtPrice.Text);
        }
    }
}
